#Maestia MMF format parser. Based on chrrox's maxscript

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class Maestia_MMF(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(Maestia_MMF,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self):
        
        charLen = self.inFile.read_short()
        return ''.join(self.inFile.read_string(charLen*2).split("\x00"))
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        totalSize = self.inFile.filesize()
        matNum = 0
        
        while self.inFile.tell() < totalSize:
            start = self.inFile.tell()
            tag = self.inFile.read_string(4)
            offset = self.inFile.read_long()
            
            if tag == "\x01\x89\xcd\xab":
                "mesh Sections"
            elif tag == "\x00\x10\x53\x50":
                "New mesh"
                self.inFile.seek(1, 1)
            elif tag == "\x11\x10\x53\x50":
                meshName = self.read_name()
                self.create_object(meshName)
            elif tag == "\x00\x41\x53\x50":
                numVerts = self.inFile.read_short()
            elif tag == "\x21\x41\x53\x50":
                for i in range(numVerts):
                    vx, vy, vz = self.inFile.read_float(3)
                    self.create_vertex(meshName, i)
                    self.add_coords(meshName, i, [vx, vy, vz])
            elif tag == "\x22\x41\x53\x50":
                for i in range(numVerts):
                    nx, ny, nz = self.inFile.read_float(3)
                    
                    self.add_vert_normals(meshName, i, [nx, ny, nz])
            elif tag == "\x23\x41\x53\x50":
                for i in range(numVerts):
                    tu, tv = self.inFile.read_float(2)
                    self.add_vert_uv(meshName, i, [tu, tv])
            elif tag == "\x00\x50\x53\x50":
                "New Face"
            elif tag == "\x10\x50\x53\x50":
                numFaces = self.inFile.read_short()
            elif tag == "\x11\x50\x53\x50":
                for i in range(numFaces):
                    v1, v2, v3 = self.inFile.read_short(3)
                    self.create_face(meshName, i)
                    self.add_face_verts(meshName, i, [v1, v2, v3])
                    self.add_face_material(meshName, i, 0)
            elif tag in ["\x12\x50\x53\x50", "\x17\x50\x53\x50"]:
                
                texName = self.read_name()
                self.create_material(matNum)
                self.add_material_name(matNum, texName[:-4])
                self.add_texture_name(matNum, texName[:-4] + ".png")
                matNum += 1
            else:
                self.inFile.seek(start + offset, 0)
            
        #self.inFile.seek(55, 1)
        
        
        #meshName = self.read_name()
        #print meshName
        
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = Maestia_MMF(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x23\x01\xCD\xAD", "MMF", "Maestia MMF"

if __name__ == '__main__':
    
    file1 = "chi_ran_f_10_glove.mmf"
    obj = read_file(file1)